package com.cms.file.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.cms.file.model.File;
import com.cms.file.service.FileService;
import com.jframe.sys.controller.ExtendAction;
import com.jframe.sys.controller.utils.DicCache;
import com.jframe.sys.controller.utils.JsBuilder;
import com.jframe.sys.utils.CalendarUtil;
import com.jframe.sys.utils.JsonUtil;
import com.jframe.sys.utils.SpringUtil;
import com.jframe.sys.utils.StringUtil;
import com.jframe.sys.webui.Columns;
import com.jframe.sys.webui.Editor;
import com.jframe.sys.webui.Grid;

public class FileAction extends ExtendAction {

	private static String ACTION="fileAction";
	private static String SERVICE = "fileService";
	private static Class<File> CLAZZ = File.class;
	public void list(){
		List<Columns> frozenColumns = new ArrayList<Columns>();// 固定列
		List<Columns> commonColumns = new ArrayList<Columns>();// 活动列
		 
		frozenColumns.add(new Columns("ck", "", 40, ""));
		//frozenColumns.add(new Columns("id", "编号", 100, ""));
		frozenColumns.add(new Columns("fileName", "文件名", 300, ""));
		frozenColumns.add(new Columns("typeId", "文件类别", 120, new Editor("combobox",true),DicCache.getComboboxJson("sm_cms_fileType")));
		commonColumns.add(new Columns("fileSize", "文件大小(KB)", 120, ""));	
		commonColumns.add(new Columns("url", "上传路径", 250, ""));
		commonColumns.add(new Columns("downCount", "下载次数", 120, ""));
		commonColumns.add(new Columns("uploadTime", "上传时间", 120, ""));
		commonColumns.add(new Columns("status", "状态", 120, new Editor("combobox",true),DicCache.getComboboxJson("dm_cms_fileStatus")));
		Grid grid = new Grid();
		grid.setUrl(this.getUrl(ACTION+".do?method=load"));
		String js = JsBuilder.getJsStr(frozenColumns, commonColumns, grid);
		this.request.setAttribute("js", js);
		this.forward("/cms/file/list/fileList.jsp");
	}
	
	public void load(){
		baseLoad(SERVICE, CLAZZ);
	}
	
	public void save() {
		baseSave(SERVICE, CLAZZ);
	}
	
	public Object beforeSave(Object obj){
		File file = (File)obj;
		java.io.File tempFile = new java.io.File(StringUtil.getPrjAbsolutePath()+file.getUrl());
		//计算文件大小转换为单位kb
		long size = tempFile.length();
		double filesize = StringUtil.parseDouble(""+size);
		filesize = filesize/1024;
		BigDecimal b = new BigDecimal(filesize);  
		filesize = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();  
		file.setFileSize(""+filesize);
		if(file.getStatus()==0){
			file.setStatus(1);
		}
		if(StringUtil.isStringNull(file.getUploadTime())){
			file.setUploadTime(CalendarUtil.getDateTime());
		}
		return file;
	}
	
	public void delete() {
		baseDelete(SERVICE);
	}
	
	public void afterDelete(String ids){

	}

	public void getObjById() {
		print(JsonUtil.obj2Json(this.getObject(SERVICE)));
	}
	
	public void setStatus(){
		String ids = request.getParameter("ids");
		int status = StringUtil.parseInt(request.getParameter("status"));
		FileService service = (FileService)SpringUtil.getBean(SERVICE);
		service.setStatus(ids, status);
		this.print(true);
	}
}
